from datetime import datetime

from pydantic import BaseModel, EmailStr, Field, ValidationError


class Email(BaseModel):
    email: EmailStr = Field(..., description='Адрес электронной почты.')


class UserModel(BaseModel):
    id: int = Field(..., description='ID пользователя.')


class User(UserModel):
    name: str = Field(..., description='Имя пользователя.')
    signup_ts: datetime = Field(default_factory=datetime.now, description='Дата и время регистрации пользователя.')
    friends: list[int] = Field(default_factory=list, description='Список идентификаторов друзей пользователя.')
    email: Email = Field(default_factory=None)


def main():

    external_data_Jack = {
        'id': '123',
        'name': 'capitan Jack sparrow',
        'signup_ts': '2024-05-27 12:22',
        'friends': [1, 2, '3'],
        'email': {'email': 'jack.sparrow@codeks.com'}
    }

    external_data_Frodo = {
        'id': '124',
        'name': 'Frodo',
        'signup_ts': '2024-05-27 12:22',
        'friends': [1, 2, '3',],
        'email': {'email': 'frodo.precious_ring.com'}
    }

    try:
        Jack = User(**external_data_Jack)
        sparrow = Jack.dict()
        print('sparrow', sparrow)
        pirate = Jack.parse_obj(sparrow)
        print('pirate', pirate)

        Frodo = User(**external_data_Frodo)
        print(Frodo.id)
        print(repr(Frodo.signup_ts))
        print(Frodo.friends)
        print(Frodo.dict())

    except ValidationError as e:
        print('Error', e.json())


if __name__ == '__main__':
    main()

